<?php
class Games_model extends Model
{
	private $category_record_count;
	/*
	*	construct
	*	@PARA: null
	*	@RETURN: null
	*/	
	public function __construct()
	{
		parent::Model();
		$this->obj =& get_instance();
		$this->initGames();		
	}
		
	/*
	*	getGamesList
	*	@PARA: null
	*	@RETURN: array of data
	*/
	public function getGamesList($category = 'ALL', $categoryID = 'ALL', $start = 0, $count = null)
	{
		$dbConfig = array();		
		//table name, foreign key
		switch ($category) 
		{
			case 'console':
				$dbConfig = array(	'tn'=>'consoles', 'fk'=>'console_id');
				break;
			case 'genre':
				$dbConfig = array(	'tn'=>'genres', 'fk'=>'genre_id');
				break;
			default:
				$dbConfig = false;
		}
		
		$results = array();
		// Build up the SQL query string and run the query
		if($dbConfig)
		{	//category has been selected
			$sql = "SELECT ga.id, ga.name, gi.image_src
					FROM ".$dbConfig['tn']." t	LEFT JOIN games ga ON ( t.id = ga.".$dbConfig['fk']." )
					LEFT JOIN game_images gi on (ga.id = gi.game_id  AND gi.image_type='S')
					WHERE 1	AND ga.id IS NOT NULL ";
			if($categoryID != 'ALL')
			{	//category id is specified
				$sql.= " AND t.id = '$categoryID' ";
			}
			$sql.= " ORDER BY t.name ASC ";
		}
		else
		{	//all of the games
			$sql = "SELECT ga.id, ga.name, gi.image_src
					FROM games ga LEFT JOIN game_images gi on (ga.id = gi.game_id AND gi.image_type='S')	
					WHERE 1	AND ga.id IS NOT NULL 
					ORDER BY ga.name ASC
					";
		}

		$countQury = $this->db->query($sql);
		$this->categoryRecordCount = $countQury->num_rows();
		unset($countQury);
		
		$limitClause = '';
		if ($count) 
		{
			$limitClause = " LIMIT $start, $count ";
		}
		else 
		{
			$limitClause = " LIMIT $start ";
		}
		$sql.= $limitClause;
		$query = $this->db->query($sql);
		if ($query->num_rows() > 0) 
		{
			foreach ($query->result_array() as $row)      // Go through the result set
			{
				// Build up a list for each column from the database and place it in
				// ...the result set
				$query_results['id']		 = $row['id'];
				$query_results['name']		 = $row['name'];
				$query_results['image_src']	 = $row['image_src'];
				$results[]		 = $query_results;
			}
		}
		return $results;
	}

	// TODO: this won't be possible if there is no primary key for the table.
	function retrieve_by_pkey($idField) 
	{
		$results = array();

		// Load  the db library
		$this->load->database();
		$query = $this->db->getwhere($this->tableName, array('id' => $idField), 1);

		if ($query->num_rows() > 0) 	
		{
			$row = $query->row_array();
			$query_results['id']			= $row['id'];
			$query_results['name']			= $row['name'];	
			$query_results['publisher']		= $row['publisher'];	
			$query_results['released_date']	= $row['released_date'];	
			$query_results['language']		= $row['language'];	
			$query_results['player_number']	= $row['player_number'];	
			$query_results['format']		= $row['format'];	
			$query_results['parental_rating']= $row['parental_rating'];	
			$results		 = $query_results;
		}
		else 
		{
			$results = false;
		}
		return $results;
	}


	/*
	*	add
	*	@PARA: $data
	*	@RETURN: id
	*/
	function add( $data ) 
	{
		// Load the database library
		$this->load->database();
		// Build up the SQL query string
		$sql = $this->db->insert_string($this->tableName, $data);
		$query = $this->db->query($sql);
		return $this->db->insert_id();
	}
		
	/*
	*	initConsoles
	*	@PARA: null
	*	@RETURN: null
	*/
	private function initGames()
	{
		$this->id = '';
		$this->name = '';		
		$this->tableName = 'games';
	}
}